<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.type" placeholder="请选择平台类型" size="medium">
                <el-option label="Main" value="Main"></el-option>
                <el-option label="LINE" value="LINE"></el-option>
                <el-option label="TELGRAM" value="TELGRAM"></el-option>
                <el-option label="WHATSAPP" value="WHATSAPP"></el-option>
            </el-select>
        </div>
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="请输入链接地址"
                      size="medium"></el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--新增按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>新增</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%">
            <el-table-column
                    prop="type"
                    label="平台类型"
                    header-align="center"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="工作区间"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{scope.row.startingTime}} - {{scope.row.endTime}}
                </template>
            </el-table-column>

            <el-table-column
                    prop="uri"
                    label="平台链接地址"
                    header-align="center"
                    align="center">
            </el-table-column>
            <el-table-column
                    sortable
                    prop="status"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.status" class="fa fa-circle text-success ml5"></i>
                    <i v-if="!scope.row.status" class="fa fa-circle text-danger ml5"></i>
                </template>
            </el-table-column>
            <el-table-column
                    prop="opAt"
                    label="创建时间"
                    header-align="center" align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="scope.row.status"
                                              :command="{type:'disable', id:scope.row.id, status:0,types:scope.row.type }">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item v-else-if="!scope.row.status"
                                              :command="{type:'disable', id:scope.row.id, status:1 ,types:scope.row.type }">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit', id:scope.row.id, row:scope.row}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'del', id:scope.row.id}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            :title="titlePopu==0?'新增客服配置':'修改客服配置'"
            :visible.sync="addDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="addForm" :rules="formRules" label-position="left" size="small"
                 label-width="80px">

            <el-form-item label="平台类型" prop="type">
                <el-select v-model="formData.type" placeholder="请选择平台类型">
                    <el-option label="Main" value="Main"></el-option>
                    <el-option label="LINE" value="LINE"></el-option>
                    <el-option label="TELGRAM" value="TELGRAM"></el-option>
                    <el-option label="WHATSAPP" value="WHATSAPP"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="工作区间" required>
                <el-col :span="4">
                    <el-form-item prop="startingTime">
                        <el-input
                                placeholder="开始时间"
                                onkeyup="value=value.replace(/[^\d]/g,'')"
                                v-model="formData.startingTime"
                                type="text"
                                oninput="if(value > 23){value = '23'}">
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="1" align="center">-</el-col>

                <el-col :span="4">
                    <el-form-item prop="endTime">
                        <el-input
                                placeholder="结束时间"
                                onkeyup="value=value.replace(/[^\d]/g,'')"
                                v-model="formData.endTime"
                                type="text"
                                oninput="if(value > 24){value = '24'}">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-form-item>


            <el-form-item prop="uri" label="链接">
                <el-input placeholder="链接"
                          v-model="formData.uri"
                          auto-complete="off"
                          type="text"
                          style="width: 100%">
                </el-input>
            </el-form-item>

            <el-form-item label="格式">
                <p style="color:#0040ff;"><span style="color:#F00;">LINE:&#12288</span>https://line.me/ti/p/xwwsM-NkXN
                </p>
                <p style="color:#0040ff;"><span style="color:#F00;">TELGRAM:&#12288</span>https://t.me/BallGoldService
                </p>
                <p style="color:#0040ff;"><span style="color:#F00;">WHATSAPP:&#12288</span>https://api.whatsapp.com/send/?phone=6283116014000&text&app_absent=0
                </p>
            </el-form-item>

            <el-form-item prop="status" label="状态">
                <span>否</span>
                <el-switch
                        v-model="formData.status"
                        active-color="#13ce66"
                        inactive-color="#ff0000">
                </el-switch>
                <span>是</span>
            </el-form-item>

        </el-form>
        <!--确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>
</div>

<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                tableData: [],
                // titlePopu=0是新增,1是修改
                titlePopu: 0,
                // 新增修改弹窗
                addDialogVisible: false,
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    searchKeyword: "",
                    searchDate: "",
                    // 平台类型
                    type: "",
                },
                searchDate: [],
                formData: {
                    // 链接
                    uri: "",
                    // 平台类型
                    type: "",
                    // 状态
                    status: false,
                    // 开始时间
                    startingTime: "",
                    // 结束时间
                    endTime: ""
                },
                formRules: {
                    uri: [
                        {required: true, message: '请输入链接', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请选择平台类型', trigger: 'change'}
                    ],
                    startingTime: [
                        {required: true, message: '请输入开始时间', trigger: 'blur'}
                    ],
                    endTime: [
                        {required: true, message: '请输入结束时间', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            // 时间格式化
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            // 页码更新操作
            pageNumberChange: function (val) {
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            // 分页大小更新操作
            pageSizeChange: function (val) {
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            // 加载分页数据
            pageData: function () {
                let self = this;
                // 显示loading
                sublime.showLoadingbar();
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }

                $.post(base + "/platform/sys/cs/account/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            // 打开新增窗口
            openAdd: function () {
                let self = this;
                self.titlePopu = 0;
                self.addDialogVisible = true;
                if (this.$refs["addForm"]) {
                    this.$refs["addForm"].resetFields();
                }
                self.formData = {
                    // 链接
                    uri: "",
                    // 类型
                    type: "",
                    // 状态
                    status: false
                };
            },
            // 监听下拉框事件
            dropdownCommand: function (command) {
                let self = this;
                // 点击修改时数据回显
                if ("edit" === command.type) {
                    self.addDialogVisible = true;
                    if (this.$refs["addForm"]) {
                        this.$refs["addForm"].resetFields();
                    }
                    //user回显
                    self.formData = {
                        id: command.row.id,
                        // 链接
                        uri: command.row.uri,
                        // 状态
                        status: command.row.status,
                        // 类型
                        type: command.row.type,
                        // 开始时间
                        startingTime: command.row.startingTime,
                        // 结束时间
                        endTime: command.row.endTime
                    };
                    self.titlePopu = 1;
                } else if ('disable' === command.type) {
                    //启、禁用
                    let str = "";
                    if (command.status === 1) {
                        str = '启用';
                    } else {
                        str = '禁用';
                    }

                    self.$confirm('确定要' + str + '该配置吗', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/sys/cs/account/edit", {
                                    id: command.id,
                                    // 类型
                                    type: command.types,
                                    status: command.status
                                }, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                } else if ("del" === command.type) {
                    // 删除按钮
                    self.$confirm('确定要删除该配置吗', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/sys/cs/account/delete", {id: command.id}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            // 新增修改
            doEdit: function () {
                let self = this;
                self.$refs["addForm"].validate(function (valid) {
                    let url = "";
                    if (eval(self.formData.startingTime) > eval(self.formData.endTime)) {
                        self.$message({
                            message: "结束时间必须大于开始时间",
                            type: 'error'
                        });
                        return;
                    }
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu === 0) {
                            url = "/platform/sys/cs/account/add";
                        } else {
                            url = "/platform/sys/cs/account/edit";
                        }
                        $.post(base + url, self.formData, function (data) {
                            if (data.code === 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.doSearch();
                                self.addDialogVisible = false;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->